#!/usr/bin/env python


import sys
import string
import re

class LogFile:
    reg = re.compile(r'([ADEI]|ERROR|INFO |DEBUG|WARN |FATAL) 200[0-9][\.-][0-9][0-9][\.-][0-9][0-9]')

    def __init__(self, the_file):
        self.last_line = None
        self.file = the_file

    def get_line(self):
        if self.last_line is None:
            line = self.file.readline()
        else:
            line = self.last_line
            self.last_line = None

        return line
    
    def put_line(self, line):
        self.last_line = line

    def get_record(self):
        record = ''

        line = self.get_line()
        record += line

        line = self.get_line()
        while 0 < len(line) and not LogFile.reg.match(line):
            record += line
            line = self.get_line()

        # read too far, so put the last line back
        self.put_line(line)
        
        if 0 < len(record):
            return record
        else:
            return None
    # end get_record
        
# end class



def sort_records(records):
    dateReg = re.compile(r'[0-9][0-9][0-9][0-9][\.-][0-9][0-9][\.-][0-9][0-9] [0-9.,:]+')
    for i in range(len(records)):
        try:
            date = dateReg.search(records[i]).group()
            date = date.replace('-', '.')
            date = date.replace(',', '.')
        except:
            print records[i]
            sys.exit(311)
        
        records[i] = (date, i, records[i])

    records.sort()

    for i in range(len(records)):
        records[i] = records[i][2]


def do_main(args):
    import glob
    files = sum((glob.glob(arg) for arg in args[1:]), [])
    
    records = []
    for filename in files:
        log_file = LogFile(open(filename, 'r'))
        
        record = log_file.get_record()
        while record:
            records.append(record)
            record = log_file.get_record()
            
    sort_records(records)
    for record in records:
        sys.stdout.write(record)

    

if __name__ == "__main__":
    do_main(sys.argv)
